import pandas as pd
import plotly.graph_objects as go #has more control, customizable
import plotly.io as pio #produce an html file
import plotly.express as px #fast, low effort
import matplotlib.pyplot as plt
import yfinance as yf
start_date = '2021-01-01'
end_date = '2023-01-01'
# Set the ticker
ticker = 'AAPL'
# Get the data
data = yf.download(ticker, start_date, end_date)
data["Date"] = data.index
data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]]
data.reset_index(drop=True, inplace=True)
[*********************100%***********************] 1 of 1 completed
data.head()
| Date | Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|---|
| 0 | 2021-01-04 | 133.520004 | 133.610001 | 126.760002 | 129.410004 | 127.874954 | 143301900 |
| 1 | 2021-01-05 | 128.889999 | 131.740005 | 128.429993 | 131.009995 | 129.455978 | 97664900 |
| 2 | 2021-01-06 | 127.720001 | 131.050003 | 126.379997 | 126.599998 | 125.098267 | 155088000 |
| 3 | 2021-01-07 | 128.360001 | 131.630005 | 127.860001 | 130.919998 | 129.367020 | 109578200 |
| 4 | 2021-01-08 | 132.429993 | 132.630005 | 130.229996 | 132.050003 | 130.483612 | 105158200 |
# Create 20 days simple moving average column
data['20_SMA'] = data["Adj Close"].rolling(20).mean()
# Create 50 days simple moving average column
data['50_SMA'] = data["Adj Close"].rolling(50).mean()
<ipython-input-4-67c785842c60>:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['20_SMA'] = data["Adj Close"].rolling(20).mean() <ipython-input-4-67c785842c60>:4: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['50_SMA'] = data["Adj Close"].rolling(50).mean()
data.head()
| Date | Open | High | Low | Close | Adj Close | Volume | 20_SMA | 50_SMA | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-01-04 | 133.520004 | 133.610001 | 126.760002 | 129.410004 | 127.874954 | 143301900 | NaN | NaN |
| 1 | 2021-01-05 | 128.889999 | 131.740005 | 128.429993 | 131.009995 | 129.455978 | 97664900 | NaN | NaN |
| 2 | 2021-01-06 | 127.720001 | 131.050003 | 126.379997 | 126.599998 | 125.098267 | 155088000 | NaN | NaN |
| 3 | 2021-01-07 | 128.360001 | 131.630005 | 127.860001 | 130.919998 | 129.367020 | 109578200 | NaN | NaN |
| 4 | 2021-01-08 | 132.429993 | 132.630005 | 130.229996 | 132.050003 | 130.483612 | 105158200 | NaN | NaN |
AAPL = go.Figure(data=[go.Candlestick(x=data['Date'],open=data['Open'],high=data['High'],low=data['Low'],close=data['Close'])])
AAPL.update_layout(title='AAPL Stock Prices From 2021 to 2023', yaxis_title='USD',xaxis_title='Time')
AAPL.add_trace(go.Scatter(x=data['Date'], y=data["20_SMA"],mode='lines',name='20_SMA',line_color='blue'))
AAPL.add_trace(go.Scatter(x=data['Date'], y=data["50_SMA"],mode='lines',name='50_SMA',line_color='orange'))
AAPL.show()
plt.rcParams['figure.figsize'] = (20,10)
plt.style.use('fivethirtyeight')
def get_wr(high, low, close, lookback):
highh = high.rolling(lookback).max()
lowl = low.rolling(lookback).min()
wr = -100 * ((highh - close) / (highh - lowl))
return wr
data['wr_14'] = get_wr(data['High'], data['Low'],data['Adj Close'], 14)
data = data.dropna()
<ipython-input-8-a41dcb614c67>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
data.head()
| Date | Open | High | Low | Close | Adj Close | Volume | 20_SMA | 50_SMA | wr_14 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 49 | 2021-03-16 | 125.699997 | 127.220001 | 124.720001 | 125.570000 | 124.265907 | 115227900 | 122.533094 | 128.426685 | -35.604262 |
| 50 | 2021-03-17 | 124.050003 | 125.860001 | 122.339996 | 124.760002 | 123.464325 | 111932600 | 122.232251 | 128.338472 | -42.011794 |
| 51 | 2021-03-18 | 122.879997 | 123.180000 | 120.320000 | 120.529999 | 119.278244 | 121229700 | 121.778017 | 128.134917 | -75.473666 |
| 52 | 2021-03-19 | 119.900002 | 121.430000 | 119.680000 | 119.989998 | 118.743843 | 185549500 | 121.289147 | 128.007829 | -79.745455 |
| 53 | 2021-03-22 | 120.330002 | 123.870003 | 120.260002 | 123.389999 | 122.108566 | 111912300 | 121.160003 | 127.862660 | -46.425376 |
ax1 = plt.subplot2grid((11,1), (0,0), rowspan = 5, colspan = 1)
ax2 = plt.subplot2grid((11,1), (6,0), rowspan = 5, colspan = 1)
ax1.plot(data['Date'],data['Adj Close'], linewidth = 2)
# ax1.plot(aapl_50, linewidth = 2,color='red')
ax1.set_title('AAPL CLOSING PRICE')
ax1.plot(data['Date'],data['20_SMA'], linewidth = 2,color='red')
ax1.plot(data['Date'],data['50_SMA'], linewidth = 2,color='orange')
ax2.plot(data['Date'],data['wr_14'], color = 'orange', linewidth = 2)
ax2.axhline(-20, linewidth = 1.5, linestyle = '--', color = 'grey')
ax2.axhline(-80, linewidth = 1.5, linestyle = '--', color = 'grey')
ax2.set_title('AAPL WILLIAMS %R 14')
plt.show()